package com.zhentao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.*;
import com.zhentao.domain.vo.Result;
import com.zhentao.domain.vo.SysUserElectricVo;
import com.zhentao.mapper.SysUserElectricMapper;
import com.zhentao.service.*;
import com.zhentao.utils.JwtService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 86133
 * @description 针对表【sys_user_electric】的数据库操作Service实现
 * @createDate 2024-08-20 17:17:50
 */
@Service
public class SysUserElectricServiceImpl extends ServiceImpl<SysUserElectricMapper, SysUserElectric>
        implements SysUserElectricService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SysElectricCardService sysElectricCardService;
    @Autowired
    private SysUserPreferentialService sysUserPreferentialService;
    @Autowired
    private SysPreferentialService sysPreferentialService;
    @Autowired
    private SysRecardService sysRecardService;

    @Override
    public Result getUserCard(String token) {
        String data = stringRedisTemplate.opsForValue().get("login:data:" + token);
        Map<String, Object> map = JwtService.getClaimsMap(data);
        Integer id = Integer.valueOf(map.get("userId").toString());
        List<SysUserElectric> list = this.list(Wrappers.<SysUserElectric>lambdaQuery().eq(SysUserElectric::getUid, id));
        if (list.size() <= 0) return null;
        for (int i = 0; i < list.size(); i++) {
            SysUserElectric even = list.get(i);
            if (even != null) {
                if (even.getElectricId() != null) {
                    SysUserElectricVo sysUserElectricVo = new SysUserElectricVo();
                    SysElectricCard one = sysElectricCardService.getOne(Wrappers.<SysElectricCard>lambdaQuery().eq(SysElectricCard::getId, even.getElectricId()));
                    sysUserElectricVo.setSysElectricCard(one);
                    BeanUtils.copyProperties(even, sysUserElectricVo);
                    list.set(i, sysUserElectricVo);
                }
            }
        }
        return Result.build(list,200,"获取成功");
    }

    @Override
    public Result addCard(String token, Integer str, Integer num, Integer juan) throws ParseException {
        String data = stringRedisTemplate.opsForValue().get("login:data:" + token);
        Map<String, Object> map = JwtService.getClaimsMap(data);
        Integer id = Integer.valueOf(map.get("userId").toString());
        //判断是否存在优惠卷，存在修改状态
        if(juan!=null){
            SysUserPreferential perferential = sysUserPreferentialService.getOne(Wrappers.<SysUserPreferential>lambdaQuery().eq(SysUserPreferential::getUserId, id).eq(SysUserPreferential::getPreferentialId, juan));
            if(BeanUtil.isEmpty(perferential)){
                return Result.build(null,400,"优惠卷不存在");
            }
            SysPreferential sysPreferential = sysPreferentialService.getById(perferential.getPreferentialId());
            Long endTime = sysPreferential.getEndTime().getTime();
            Long date = new Date().getTime();
            if(endTime-date<0){
                return Result.build(null,400,"优惠卷已过期");
            }
        }
        //根据电卡字符串查询电卡id
        SysElectricCard one = sysElectricCardService.getOne(Wrappers.<SysElectricCard>lambdaQuery().eq(SysElectricCard::getId, str));
        SysUserElectric sysUserElectric = new SysUserElectric();
        Date date = new Date();
        String da = (1900+date.getYear()) + "年" + (dataFormat(date.getMonth() + 1)) + "月" + dataFormat(date.getDate()) + "日";
        System.out.println(da);
        Date parse = new SimpleDateFormat("yyyy年MM月dd日").parse(da);
        sysUserElectric.setUid(id);sysUserElectric.setElectricId(one.getId());sysUserElectric.setStartTime(new Date());sysUserElectric.setUseNum(num);sysUserElectric.setEndTime(parse);
        return this.save(sysUserElectric)?Result.build(null,200,"添加成功"):Result.build(null,400,"添加失败");
//        SysRecard sysRecard = new SysRecard();
//        sysRecard.setUid(id); sysRecard.setCid(sysUserElectric.getId());sysRecard
//        sysRecardService.save()
    }
    public String dataFormat(Integer integer){
        String str;
        if(integer<=9){
            str="0"+integer;
        }else {
            str = String.valueOf(integer);
        }
        return str;
    }
}




